/*
 * @Author: 肖思汗 
 * @Date: 2025-06-02 12:11:13 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-09-11 02:50:51
 */
import { cloneDeep } from "lodash";
import { observable, configure } from "mobx";
configure({ enforceActions: "never" });
const designerHistory = observable({

    // 简易的历史记录栈
    historyEnable: true,
    stackPointer: -1, // 数据指针
    undoStack: [], // 历史记录栈

    hasUndo: false,
    hasRedo: false,

    //重置历史记录
    resetHistory() {
        this.undoStack = []; // 清空历史记录栈
        this.stackPointer = 0; // 数据指针指向最后一个数据 
        this.hasUndo = false;
        this.hasRedo = false;
    },

    // 记录历史
    recordHistory(json) {
        if (this.historyEnable) {
            this.undoStack = this.undoStack.slice(0, this.stackPointer + 1);
            this.undoStack.push(cloneDeep(json));
            this.stackPointer = this.undoStack.length - 1; // 数据指针指向最后一个数据

            this.hasUndo = (this.stackPointer > 0);
            this.hasRedo = (this.stackPointer < this.undoStack.length - 1);
        }

    },

    // 撤销操作
    undo() {
        if (this.stackPointer > 0) {
            this.stackPointer--;
            this.hasUndo = (this.stackPointer > 0);
            this.hasRedo = (this.stackPointer < this.undoStack.length - 1);
            const json = this.undoStack[this.stackPointer];
            return cloneDeep(json);
        }
    },

    // 重做操作
    redo() {
        if (this.stackPointer < this.undoStack.length - 1) {
            this.stackPointer++;
            this.hasUndo = (this.stackPointer > 0);
            this.hasRedo = (this.stackPointer < this.undoStack.length - 1);
            const json = this.undoStack[this.stackPointer];
            return cloneDeep(json);
        }
    }

}, {}, { deep: false });

export default designerHistory;

